﻿using System;
using System.Collections.Generic;
using System.Text;

namespace YbxLibrary
{
    public class Json
    {
        #region 固定值
        public const string _mdtrtarea_admvs = "420300";
        public const string _insuplc_admdvs = "420300";
        public const string _opter = "00000";
        public const string _opter_name = "全院";
        public const string _sign_no = "111111111111111111111111111111";
        public const string _fixmedins_code = "H42030200820";
        public const string _fixmedins_name = "十堰市太和医院";
        public const string _infver = "V2.0";
        public const string _opter_type = "1";
        public const string _recer_sys_code = "win";      
        public const string _app_id = "";
        public const string _enc_type = "";
        public const string _pw_ecToken = "";
        public const string _dev_no = "";
        public const string _dev_safe_info = "";
        public const string _cainfo = "";
        public const string _signtype = "";
        #endregion 

        /// <summary>
        ///  构造入参固定值
        /// </summary>.
        /// <param name="infno">交易编号</param>
        /// <param name="input">数据体</param>
        /// <param name="opter_input">工号</param>
        /// <param name="opter_name_input">姓名</param>
        /// <param name="mdtrtarea_admvs_input">就医地</param>
        /// <param name="insuplc_admdvs_input">参保地</param>
        /// <param name="sign_no_input">交易签到流水号</param>
        /// <returns></returns>
        public static string BuildJsonStringFix(string infno, string input, string opter_input, string opter_name_input, string mdtrtarea_admvs_input, string insuplc_admdvs_input, string sign_no_input)
        {
            string currentDirectory = AppDomain.CurrentDomain.BaseDirectory;

            #region   初始化
            string mdtrtarea_admvs = mdtrtarea_admvs_input=="" ? _mdtrtarea_admvs : mdtrtarea_admvs_input;
            string insuplc_admdvs = insuplc_admdvs_input=="" ? _insuplc_admdvs: insuplc_admdvs_input;
            string opter = opter_input == "" ? _opter : opter_input;
            string opter_name = opter_name_input == "" ? _opter_name : opter_name_input;
            string sign_no = sign_no_input == "" ? _sign_no : sign_no_input;

            string fixmedins_code = _fixmedins_code;
            string fixmedins_name = _fixmedins_name;
            string infver = _infver;
            string recer_sys_code = _recer_sys_code;
            string opter_type = _opter_type;


            string app_id = _app_id;
            string enc_type = _enc_type;
            string pw_ecToken = _pw_ecToken;
            string dev_no = _dev_no;
            string dev_safe_info = _dev_safe_info;
            string cainfo = _cainfo;
            string signtype = _signtype;
            #endregion

            #region 变量参数
            DateTime current_time = DateTime.Now;
            string randfour =  Convert.ToString(new Random().Next(1000, 9999));
            #endregion

            Dictionary<string, string> jsonDict = new Dictionary<string, string>
            {
            { "infno", infno },
            { "msgid", fixmedins_code+current_time.ToString("yyyyMMddHHmmss")+randfour },
            { "mdtrtarea_admvs", mdtrtarea_admvs },
            { "insuplc_admdvs", insuplc_admdvs },
            { "recer_sys_code", recer_sys_code},
            { "dev_no", dev_no },
            { "dev_safe_info", dev_safe_info },
            { "cainfo", cainfo },
            { "signtype", signtype },
            { "infver", infver },
            { "opter_type", opter_type },
            { "opter", opter},
            { "opter_name", opter_name },
            { "inf_time", current_time.ToString("yyyy-MM-dd HH:mm:ss") },
            { "fixmedins_code", fixmedins_code},
            { "fixmedins_name", fixmedins_name },
            { "sign_no", sign_no},
            { "app_id", app_id },
            { "enc_type", enc_type },
            { "pw_ecToken", pw_ecToken }
        };

            StringBuilder jsonString = new StringBuilder();
            jsonString.Append("{");

            bool first = true;
            foreach (var pair in jsonDict)
            {
                if (!first)
                {
                    jsonString.Append(",");
                }
                jsonString.Append("\""+pair.Key+"\"");
                jsonString.Append(":");
                jsonString.Append("\"" + pair.Value+"\"" );
                first = false;
            }
            jsonString.Append(",");
            jsonString.Append("\"input\":");
            jsonString.Append(input);
            jsonString.Append("}");

            return jsonString.ToString();
        }

        /// <summary>
        ///  构造入参读取配置文件
        /// </summary>.
        /// <param name="infno">交易编号</param>
        /// <param name="input">数据体</param>
        /// <param name="opter_input">工号</param>
        /// <param name="opter_name_input">姓名</param>
        /// <param name="mdtrtarea_admvs_input">就医地</param>
        /// <param name="insuplc_admdvs_input">参保地</param>
        /// <param name="sign_no_input">交易签到流水号</param>
        /// <returns></returns>
        public static string BuildJsonString(string infno, string input, string opter_input, string opter_name_input, string mdtrtarea_admvs_input, string insuplc_admdvs_input, string sign_no_input)
        {
            #region
            string mdtrtarea_admvs = RealData(mdtrtarea_admvs_input, "mdtrtarea_admvs");
            string insuplc_admdvs = RealData(insuplc_admdvs_input, "insuplc_admdvs");
            string opter = RealData(opter_input, "opter");
            string opter_name = RealData(opter_name_input, "opter_name");
            string sign_no = RealData(sign_no_input,"sign_no");
            string fixmedins_code = RealData(null, "fixmedins_code");
            string fixmedins_name = RealData(null, "fixmedins_name");
            string infver = RealData(null, "infver");
            string recer_sys_code = RealData(null, "recer_sys_code");
            string opter_type = RealData(null, "opter_type");
            string app_id = RealData(null, "app_id");
            string enc_type = RealData(null, "enc_type");
            string pw_ecToken = RealData(null, "pw_ecToken");
            string dev_no = RealData(null, "dev_no");
            string dev_safe_info = RealData(null, "dev_safe_info");
            string cainfo = RealData(null, "cainfo");
            string signtype = RealData(null, "signtype");
            #endregion

            #region 变量参数
            DateTime current_time = DateTime.Now;
            string randfour = Convert.ToString(new Random().Next(1000, 9999));
            #endregion

            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("infno", infno);
            dict.Add("msgid", fixmedins_code+current_time.ToString("yyyyMMddHHmmss")+randfour);
            dict.Add("mdtrtarea_admvs", mdtrtarea_admvs);
            dict.Add("insuplc_admdvs", insuplc_admdvs);
            dict.Add("recer_sys_code", recer_sys_code);
            dict.Add("dev_no", dev_no);
            dict.Add("dev_safe_info", dev_safe_info);
            dict.Add("cainfo", cainfo);
            dict.Add("signtype", signtype);
            dict.Add("infver", infver);
            dict.Add("opter_type", opter_type);
            dict.Add("opter", opter);
            dict.Add("opter_name", opter_name);
            dict.Add("inf_time", current_time.ToString("yyyy-MM-dd HH:mm:ss"));
            dict.Add("fixmedins_code", fixmedins_code);
            dict.Add("fixmedins_name", fixmedins_name);
            dict.Add("sign_no", sign_no);
            dict.Add("app_id", app_id);
            dict.Add("enc_type", enc_type);
            dict.Add("pw_ecToken", pw_ecToken);

            StringBuilder jsonString = new StringBuilder();
            jsonString.Append("{");

            bool first = true;
            foreach (var pair in dict)
            {
                if (!first)
                {
                    jsonString.Append(",");
                }
                jsonString.Append("\"" + pair.Key + "\"");
                jsonString.Append(":");
                jsonString.Append("\"" + pair.Value + "\"");
                first = false;
            }
            jsonString.Append(",");
            jsonString.Append("\"input\":");
            jsonString.Append(input);
            jsonString.Append("}");

            return jsonString.ToString();
        }

        /// <summary>
        /// 取值
        /// </summary>
        /// <param name="data"></param>
        /// <param name="node"></param>
        /// <returns></returns>
        private static string RealData(string data,string node) {
            string current = AppDomain.CurrentDomain.BaseDirectory;
            IniFileUtils iniFile = new IniFileUtils(current + "yb.ini");
            return string.IsNullOrEmpty(data) ? iniFile.ReadValue("general", node) : data;
        }
    }
}
